import std.string, std.c.stdlib;

extern (C) int rose_spawn_pipe(char **argv, int pin, int pout);
extern (C) int rose_spawn_pipe_chain(char ***argv, int n);
extern (C) int rose_wait(int pid);
extern (C) int rose_wait_chain(int n, int pid);

int spawnPipe(string[] argv, int pin, int pout)
{
	char **buf = cast(char **) malloc(argv.length*(char *).sizeof);
	for (int i = 0; i < argv.length; i++)
		buf[i] = cast(char *) std.string.toStringz(argv[i]);
	int e = rose_spawn_pipe(buf, pin, pout);
	free(buf);
	return e;
}

int spawnPipeChain(string[][] argv)
{
	char ***buf = cast(char ***) malloc(argv.length*(char **).sizeof);
	for (int i = 0; i < argv.length; i++)
	{
		char **tmp = cast(char **) malloc(argv[i].length*(char *).sizeof);
		for (int j = 0; j < argv[i].length; j++)
			tmp[j] = cast(char *) std.string.toStringz(argv[i][j]);
		buf[i] = tmp;
	}
	int e = rose_spawn_pipe_chain(buf, argv.length);
	for (int i = 0; i < argv.length; i++)
		free(buf[i]);
	free(buf);
	return e;
}

int wait(int pid)
{
	return rose_wait(pid);
}

int waitChain(int n, int pid)
{
	return rose_wait_chain(n, pid);
}

